﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Easy.Common;
using Easy.BLL;
using Easy.Operation;

namespace Easy.Web.Service.Data
{
    /// <summary>
    /// GetNextPageData 的摘要说明
    /// </summary>
    public class GetNextPageData : BaseHttpHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string returnValue = "";
            try
            {
                
                string entityName = context.Request["entityname"];  //实体名
                string order = context.Request["order"];    //排序字段名
                string filterData = context.Request["filter"];  //筛选器
                string ViewName = context.Request["viewname"];  //视图名
                if (string.IsNullOrWhiteSpace(ViewName)) ViewName = "DefaultView";
                int pageSize = 10;  //获取记录条数
                if (!string.IsNullOrWhiteSpace(context.Request["pagesize"])) 
                    pageSize = int.Parse(context.Request["pagesize"].ToString());
                Guid lastid = Guid.Empty;    //临界点GUID
                if (!string.IsNullOrWhiteSpace(context.Request["lastid"])) 
                    lastid = new Guid(context.Request["lastid"]);
                string lastordervalue = context.Request["lastordervalue"]; //临界点排序字段值

                DataService service = new DataService(entityName, this.CurrentUserId);
                returnValue = service.RetrieveNextPage(ViewName, filterData, order, pageSize, lastordervalue, lastid);
            }
            catch (CustomException cusEx)
            {
                returnValue = JsonConvert.SerializeObject(cusEx.CustomResult);
            }
            catch(Exception ex)
            {
               CustomResult cusResult = new CustomResult();
               cusResult.ResultCode = -1;
               cusResult.ResultMessage = ex.Message;
               cusResult.ResultData = ex.Message;
               returnValue = JsonConvert.SerializeObject(cusResult);
            }
            context.Response.Write(returnValue);
        }

    }
}